package com.qqqq.code;

import com.qqqq.domain.User;
import com.qqqq.utils.ResponseUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//import javax.xml.ws.Response;
import java.io.IOException;
import java.sql.SQLException;


public class BaseHttpServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        super.service(req, resp);
    }

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        res.setCharacterEncoding("utf-8");
        super.service(req, res);
    }


    public void executeBusiness(HttpServletResponse resp, IServletBusinessExecute servletBusinessExecute) throws IOException {
        try {
            servletBusinessExecute.execute(resp);
        } catch (SQLException e) {
            e.printStackTrace();
            ResponseUtils.responseJson(resp, AjaxResult.error(500,"SQL查询异常"));
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtils.responseJson(resp, AjaxResult.error());
        }
    }
}
